package com.yihen.controller;

import cn.hutool.core.bean.BeanUtil;
import com.yihen.dto.LoginFormDTO;
import com.yihen.dto.Result;
import com.yihen.dto.UserDTO;
import com.yihen.dto.UserLevelDTO;
import com.yihen.pojo.User;
import com.yihen.service.UserService;
import com.yihen.util.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/users")
@Slf4j
public class UserController {
    @Resource
    private UserService userService;

    // 登陆验证
    @PostMapping("/login")
    public Result login(@RequestBody LoginFormDTO loginForm) {

        return userService.login(loginForm.getPhone(), loginForm.getPassword());
    }

    // 退出登陆
    @GetMapping("/logout")
    public Result login() {
        return userService.logout();
    }

    // 获取个人信息
    @GetMapping("/me")
    public Result me() {
        //  获取当前登录的用户并返回

        return userService.getInformation();
    }

    // 修改用户密码
    @PostMapping("/updatepassword/{password}/{newpassword}")
    public Result updatePassword(@PathVariable String password, @PathVariable String newpassword) {
        return userService.updatePassword(password, newpassword);
    }


    // 获取所有个人信息
    @GetMapping("/all")
    public Result all() {
        return userService.getAllUser();
    }

    // 添加用户
    @PostMapping("/add")
    public Result addUser(@RequestBody User user) {
        return userService.addUser(user);
    }

    // 修改用户权限
    @PostMapping("/update/level")
    public Result updateUserLevel(@RequestBody UserLevelDTO user) {
        return userService.updateUserLevel(user.getLevel(), user.getUserId());
    }

    // 条件查询用户
    @GetMapping("select/{mode}/{condition}")
    public Result getUserByCondition(@PathVariable int mode, @PathVariable String condition) {
        return userService.selectUserByCondition(mode, condition);
    }

    //  删除用户
    @DeleteMapping("/delete/{userId}")
    public Result deleteUserByUserId(@PathVariable Long userId) {
        return userService.deleteUser(userId);
    }

    // 修改用户信息
    @PostMapping("/update/information")
    public Result updateUser(@RequestBody User user) {
        return userService.updateUser(user);
    }

    // 获取个人发布数量类别
    @GetMapping("/category")
    public Result getCategories() {
        return userService.countCategory();
    }

    // 获取个人订单评价数量
    @GetMapping("/appraise")
    public Result getAppraises() {
        return userService.countAppraise();
    }

    // 获取个人交易数量
    @GetMapping("/deals")
    public Result getDeals() {
        return userService.countDeals();
    }

    // 批量查询用户
    @PostMapping("/others")
    public Result getOthers(@RequestBody List<Long> otherIds){
        Result result = userService.selectUsersByIds(otherIds);
        return result;
    }
}
